jQuery(document).ready(function($) {
	
	// 
	// var address = "4414 SE 28th Ave, Portland OR";
	// 
	// jQuery('#map1').children(".map").jmap('SearchAddress', {
	//             'query': address,
	//             'returnType': 'getLocations'
	//         }, function(result, options) {
	// 
	// 			var center = [point.Point.coordinates[1], point.Point.coordinates[0]];
	// 			var address = point.address;
	//             // var valid = Mapifies.SearchCode(result.Status.code);
	//             // if (valid.success) {
	// 				var center = [point.Point.coordinates[1], point.Point.coordinates[0]];
	// 				var address = point.address;
	//             // jQuery.each(result.Placemark, function(i, point){
	//             //     jQuery('#map1').children(".map").jmap('AddMarker',{
	//             //             'pointLatLng':[point.Point.coordinates[1], point.Point.coordinates[0]],
	//             //             'pointHTML':point.address
	//             //         });
	//             //     });
	//             // } else {
	//                 alert(valid.message);
	//             }
	//         });	
	
	
	
	myIcon = Mapifies.createIcon({
			iconImage: "http://swiftcollective.com/bd/gicon.png",
			iconShadow: "http://swiftcollective.com/bd/gicon_trans.png",
			iconSize: new GSize(25, 26),
			iconShadowSize: new GSize(25, 26),
			iconAnchor: new GPoint(1, 26),
			iconInfoWindowAnchor: new GPoint(1, 26),
			iconPrintImage: "http://swiftcollective.com/bd/gicon_trans.png",
			iconMozPrintImage: "http://swiftcollective.com/bd/gicon_trans.png",
			iconPrintShadow: "http://swiftcollective.com/bd/gicon_trans.png",
			iconTransparent: "http://swiftcollective.com/bd/gicon_trans.png"
	});	

	if ($('.lat').size() == 1 && $('.lng').size() == 1) {
		var lat = $(".lat").text();
		var lng = $(".lng").text();
		var zoom = 9;
	} else if ($('.lat').size() == 0 && $('.lng').size() == 0) {
		var nomap = true;
	} else {
		var lat = 33.5;
		var lng = -95;
		var zoom = 2;
	}

		jQuery('.map').jmap('init', {
		'pointIcon': myIcon,
		'mapType':'map',
		// "mapEnableInfoWindows":false,
		"mapEnableDragging":true,
		"mapEnableType":true, 
		'mapCenter':[lat, lng], 
		"mapZoom":zoom, 
		"mapEnableDoubleClickZoom":true,
		"mapEnableSmoothZoom":true,
		"mapShowjMapsIcon":false,
		// "centerMap":true,
		// "mapControl":"none",
		// "mapEnableScaleControl":false,
		"centerMoveMethod":"pan"
		});
		

	function doSomething() {
		var size = jQuery(".map").parent().width() + 'px';
		jQuery('.map').animate({'width': size, 'height': "263px"}, 0, function(){
			jQuery(this).jmap('CheckResize');
		});
	};

	var resizeTimer = null;
	$(window).bind('resize', function() {
	    if (resizeTimer) clearTimeout(resizeTimer);
	    resizeTimer = setTimeout(doSomething, 10);
	});

	$('.latest_entries li').each(function(index) {
		lat = $(this).children().children(".lat").text();
		lng = $(this).children().children(".lng").text();
		
		contents = $(this).html();
		title = "<div class='mapdetail'>"+contents+"</div>";

		jQuery('#map1').children(".map").jmap('AddMarker', {
					'pointIcon': myIcon,
					'pointHTML': title,
					'pointLatLng': [lat,lng]
				}, function( marker, options){
					jQuery('.remove-marker').click(function(){
						jQuery('#map1').jmap('RemoveMarker', marker);
					});
					
				});
	});
	
	$('#article_map').each(function(index) {
		lat = $(this).children(".lat").text();
		lng = $(this).children(".lng").text();

		jQuery('#article_map .map').jmap('AddMarker', {
				'pointIcon': myIcon,
				'pointLatLng': [lat,lng]
			}, function( marker, options){
				jQuery('.remove-marker').click(function(){
				jQuery('#article_map .map').jmap('RemoveMarker', marker);
			});
		});
					

		// if ($('.trek').size() > 0) {
		// 	var trek = $('.trek').text()
		// 	jQuery(this).children(".map").jmap('AddPolyline', {
		// 		'polylinePoints': [[28.41939, -81.58096],[28.43939, -81.57096],[28.45939, -81.58096],[28.46939, -81.56096],[28.46939, -81.58096]],
		// 		'polylineStrokeColor':"#f57a22",
		// 		'polylineStrokeWidth':2
		// 	}, function( polyline, options){
		// 		jQuery('.remove-polygon').click(function(){
		// 			jQuery('#map1').jmap('RemovePolygon', polygon);
		// 		});
		// 	})
		// };


	
	});
	
	
	
});
